package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.PmsBrand;
import com.itheima.jsonresult.JsonResult;
import com.itheima.mapper.PmsBrandMapper;
import com.itheima.service.PmsBrandService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author linying
 * @since 2022-10-13
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {

    /**
     * 品牌列表
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult getBrandList(String name, Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        // 姓名可选非空条件判断
        if (!StrUtil.isBlank(name)) {
            wrapper.eq("name", name);
        }
        // 调用分页查询方法
        Page jsonpage = (Page) page(new Page<>(page, pageSize), wrapper);

        return JsonResult.success(jsonpage);
    }

    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @Override
    public JsonResult addBrand(PmsBrand brand) {
        // 根据username查询是否存在
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", brand.getName());

        // 查询结果为空，不存在
        if ((getOne(wrapper) == null)) {
            // 添加品牌
            save(brand);

            return new JsonResult(1, null, "添加品牌成功", null);
        } else {
            return new JsonResult(0, null, brand.getName() + "已存在", null);
        }
    }
}
